package com.example.allplan.MyDialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.allplan.JavaBean.JiHuaTiaoJavaBean;
import com.example.allplan.R;

import java.util.Objects;

public class ShuRuJiHuaDialog extends DialogFragment {
    View mview;
    onfinish mlisten;
    public JiHuaTiaoJavaBean mshuru;
    EditText shu_ru_ming_cheng_edittext,shu_ru_shi_jian_edittext;
    Button finish_button;
    public interface onfinish{
        void onFinish(JiHuaTiaoJavaBean o);
    }
    public static ShuRuJiHuaDialog newInstance(){

        ShuRuJiHuaDialog m=new ShuRuJiHuaDialog();
        return m;
    }
    public void setOnFinish(onfinish m){
        this.mlisten=m;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mview=inflater.inflate(R.layout.shu_ru_ji_hua_dialog,container,false);
        return mview;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable( new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        Objects.requireNonNull(getActivity()).getWindowManager().getDefaultDisplay().getMetrics( dm );

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width =  ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        shu_ru_ming_cheng_edittext=mview.findViewById(R.id.dialog_editname);
        shu_ru_shi_jian_edittext=mview.findViewById(R.id.dialog_edittime);
        shu_ru_ming_cheng_edittext.setText(mshuru.getMingcheng());
        shu_ru_shi_jian_edittext.setText(mshuru.getChu_shi_zhi()+"");
        finish_button=mview.findViewById(R.id.finish_button);
        finish_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mlisten!=null){
                    if(shu_ru_ming_cheng_edittext.getText().toString().equals("")||shu_ru_shi_jian_edittext.getText().toString().equals("")){
                        Toast.makeText(getActivity(), "请输入内容", Toast.LENGTH_SHORT).show();
                    }else if(new Integer(shu_ru_shi_jian_edittext.getText().toString()).intValue()<=0){
                        Toast.makeText(getActivity(), "时间必须大于0", Toast.LENGTH_SHORT).show();
                    }else{
                        mshuru.setMingcheng(shu_ru_ming_cheng_edittext.getText().toString());
                        mshuru.setChu_shi_zhi(new Integer(shu_ru_shi_jian_edittext.getText().toString()).intValue());
                        mshuru.setZan_cun_zhi(new Integer(shu_ru_shi_jian_edittext.getText().toString()).intValue());
                        mshuru.setXian_zai_zhi(new Integer(shu_ru_shi_jian_edittext.getText().toString()).intValue());
                        dismiss();
                        mlisten.onFinish(mshuru);
                    }
                }
            }
        });
    }
}
